package com.md.infrastructure.value;

import com.md.base.EnumBase;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 异常规则: <p/>在一个 module 下 code 递增(1),不同 module 下 code 递增(10000)
 *
 * @author 刘骄阳
 * 2022-09-21 21:35
 */
@Getter
@AllArgsConstructor
public enum ErrorEnum implements EnumBase {
  REGISTER_INFO_ILLEGAL(10000, "%s不合法"),
  MESSAGE_IS_EXISTS(10001, "%s已存在"),
  USERS_NOT_EXIST(10002, "用户不存在"),
  USERS_NOT_PERMISSION(10003, "暂无操作权限"),
  ROLE_NOT_EXIST(10004, "角色不存在");

  private static final Map<Integer, ErrorEnum> ENUM_MAP = Arrays.stream(ErrorEnum.values())
    .collect(Collectors.toMap(EnumBase::getCode, e -> e));
  private final Integer code;
  private final String message;

  public static ErrorEnum of(final Integer code) {
    return ENUM_MAP.get(code);
  }
}
